package com.example.springmvcinterview;

/**
 * @Author : 叶荻风
 * @create : 2025/1/14 19:01
 */
public class ThreadLocalDemo {
    public static ThreadLocal<String> threadLocal = new ThreadLocal<>();
    //线程隔离,互不影响
    public static void main(String[] args) {
        threadLocal.set("hello main");
        threadLocal.get();
        System.out.println("创建新线程前，主线程" + Thread.currentThread().getName() + "的threadlocal字符值为："  + threadLocal.get());
        try {
            Thread thread = new Thread(() -> {
                ThreadLocalDemo.threadLocal.set("new thread");
                System.out.println("新线程" + Thread.currentThread().getName() + "的threadlocal字符值为：" + ThreadLocalDemo.threadLocal.get());
            });
            thread.start();
            thread.join();
        } catch (Exception e) {
            System.out.println(e);
        }
        System.out.println("创建新线程后，主线程" + Thread.currentThread().getName() + "的threadlocal字符值为："  + ThreadLocalDemo.threadLocal.get());
    }
}
